
/*
 * @method 日期简单格式化避免引入moment大体量类库
 * @params format
 *
 */
Date.prototype.format = function(format) {
    var o = {
      "M+": this.getMonth() + 1, //month
      "d+": this.getDate(), //day
      "h+": this.getHours(), //hour
      "m+": this.getMinutes(), //minute
      "s+": this.getSeconds(), //second
      "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
      "S": this.getMilliseconds() //millisecond
    }
  
    if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
  
    let i18n = window.i18n || null;
  
    var week = {
      "1":  "周一",
      "2":  "周二",
      "3":  "周三",
      "4":  "周四",
      "5":  "周五",
      "6":  "周六",
      "0":  "周日"
    };
  
    // if (/(E+)/.test(format)) {
    //   format = format.replace(RegExp.$1, week[this.getDay() + ""]);
    // }
  
    for (var k in o) {
      if (new RegExp("(" + k + ")").test(format)) {
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
      }
    }
  
    if (/(E+)/.test(format)) {
      format = format.replace(RegExp.$1, week[this.getDay() + ""]);
    }
  
    return format;
  }
//
// @function 给对象移除空字段
//

function removeBlankField(obj) {
  let temp = {};
  Object.keys(obj).forEach((key)=>{
    if(typeof obj[key] !== "undefined"){
      temp[key] = obj[key]
    }
  })
  return temp;
};

//没有 formatter，就默认都补一个 formatter
function addOmitFormatter(conditiones){
    conditiones.forEach((e,i,array)=>{
        if(!e.formatter){
            //没有 formatter，就默认都补一个 formatter
            array[i].formatter = function(row, column){
                if(!row[e.prop]){
                    return '--';
                }else{
                    return row[e.prop];
                }
                
            }
        }
    })
    return conditiones;
}
function  conver(limit){
    var size = "";
    if( limit < 0.1 * 1024 ){ //如果小于0.1KB转化成B
        size = limit.toFixed(2) + "B";
    }else if(limit < 0.1 * 1024 * 1024 ){//如果小于0.1MB转化成KB
        size = (limit / 1024).toFixed(2) + "KB";
    }else if(limit < 0.1 * 1024 * 1024 * 1024){ //如果小于0.1GB转化成MB
        size = (limit / (1024 * 1024)).toFixed(2) + "MB";
    }else{ //其他转化成GB
        size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB";
    }

    var sizestr = size + "";
    var len = sizestr.indexOf("\.");
    var dec = sizestr.substr(len + 1, 2);
    if(dec == "00"){//当小数点后为00时 去掉小数部分
        return sizestr.substring(0,len) + sizestr.substr(len + 3,2);
    }
    return sizestr;
}
export { removeBlankField, addOmitFormatter, conver };

/**
 * 字符串字符数量 汉字等是两个字符
 * @param str
 */
export function getLength(str) {
    let result = 0;
    let length = str.length;
    while (length--) {
      if (/^[\u0000-\u00ff]$/.test(str.charAt(length))) {
        result += 1;
      } else {
        result += 2;
      }
    }
  
    return result;
  }
  
  /**
   * 字符串截取 英文*2
   * @param str maxLength
   */
  export function substr(str, maxLength) {
    let re = new RegExp("^[a-zA-Z]|[0-9]+$");
    let length = str.length;
    let result = 0;
    let index = 0;
  
    for(let i = 0; i < str.length; i++){
      if (/^[\u0000-\u00ff]$/.test(str.charAt(i))) {
        result += 1;
      } else {
        result += 2;
      }
      if(result >= maxLength * 2){
        index = i + 1
        break
      }
    }
  
    /**
     * 如果是开头英文最后是中文的正则检测为true
     * 此时字符串截取就出错了
     * 所以改为上面的方法
  
     if (re.test(str)) {
       maxLength = maxLength * 2;
     }
     *
     */
  
    return str.substr(0, index);
  }

  /**
   * 统计字数工具函数
   * 
   */
export function countCharacters(Words) {
    var _words = Words.replace(/(^\s*)|(\s*$)/g, ""); //去掉前后空格
    _words = _words.replace(/^(\s*)\r\n/g, "");//去除空行

    var sTotal = 0;//双字节的字符——全角字符个数
    var iTotal = 0;//汉字字符个数
    var eTotal = 0;//非双字节的字符——半角字符,字母,数字,空格个数
    
    for (let i = 0; i < _words.length; i++) {
        var c = _words.charAt(i);
        if (c.match(/[\u4e00-\u9fa5]/) || c == ' ') { //中文,空格
            iTotal++;
        }else if (c.match(/[^\x00-\xff]/)) {  //双字节的字符——全角字符
            sTotal++;
        } else {  //半角字符,字母,数字
            eTotal++;//非双字节的字符
        }
    }
    return iTotal + sTotal + eTotal / 2;
}


export function getPlatformKey() {
  let key = 'rain';
  let host = {
    'www.yuketang.cn': 'rain',
    'b.yuketang.cn': 'thunder',
    'pro.yuketang.cn': 'thu',
    'changjiang.yuketang.cn': 'changjiang',
    'g.yuketang.cn': 'g',
    'huanghe.yuketang.cn': 'huanghe',
    'pro.xuetangonline.com': 'protest',
    'protest.xuetangonline.com': 'protest'
  }

  key = host[location.host] || '';

  return key;
}

export function encodeHTNML(str) {
  if (str.length == 0) return str;
  str = str.replace(/</g, '&lt;');
  str = str.replace(/>/g, '&gt;');
  return str;
}


